<?php
class page
{
	//var $module;
	var $db;
	var $table;
	var $page;
	var $menu;
	var $u;

	function __construct(/*$module = 'page'*/)
	{
		global $db, $PAGE;
		$this->db = &$db;
		$this->table = DB_PRE.'page';
		$this->page = $PAGE;
		//$this->module = $module;
		//$this->menu = load('menu.class.php','menu');
		$this->u = load('url.class.php','url');
	}

	/*function page($module = 'page')
	{
		$this->__construct($module);
	}
*/
	function get($pageid)
	{
		$data = $this->db->get_one("SELECT * FROM `$this->table` WHERE `pageid`=$pageid");
		if(!$data) return false;
		if($data['setting'])
		{
			$setting = $data['setting'];
			eval("\$setting = $setting;");
			unset($data['setting']);
			if(is_array($setting)) $data = array_merge($data, $setting);
		}
		return $data;
	}

	function add($page, $setting = array())
	{
		if(!is_array($page)) return FALSE;
		//$page['module'] = $this->module;
		$this->db->insert($this->table, $page);
		$pageid = $this->db->insert_id();
		if($page['parentid'])
		{
			$page['arrparentid'] = $this->page[$page['parentid']]['arrparentid'].','.$page['parentid'];
			$page['parentdir'] = $this->page[$page['parentid']]['parentdir'].$this->page[$page['parentid']]['catdir'];
			$parentids = explode(',', $page['arrparentid']);
			foreach($parentids as $parentid)
			{
				if($parentid)
				{
					$arrchildid = $this->page[$parentid]['arrchildid'].','.$pageid;
					$this->db->query("UPDATE `$this->table` SET child=1,arrchildid='$arrchildid' WHERE pageid='$parentid'");
				}
			}
		}
		else
		{
			$page['arrparentid'] = '0';
			$page['parentdir'] = '';
		}
		$arrparentid = $page['arrparentid'];
		$parentdir = $page['parentdir'];
		$this->db->query("UPDATE `$this->table` SET `arrchildid`='$pageid',`listorder`=$pageid,`arrparentid`='$arrparentid',`parentdir`='$parentdir' WHERE pageid=$pageid");
		if($setting) setting_set($this->table, "pageid=$pageid", $setting);
		
		/*if($this->module == 'page' && $page['type'] < 2)
		{*/
			$parentid = $page['parentid'];
			
			//$this->menu->update('pageid_'.$pageid, array('parentid'=>$this->menu->menuid('pageid_'.$parentid), 'name'=>$page['catname'], 'url'=>'?module=page&action=manage&pageid='.$pageid));
			//if($parentid) $this->menu->update('pageid_'.$parentid, array('url'=>''));
		//}
		if($page['type'] == 2)
		{
			return true;
		}
		else
		{
			$this->url($pageid);
		}
		return $pageid;
	}

	function edit($pageid, $page, $setting = array())
	{
		$parentid = $page['parentid'];
		$oldparentid = $this->page[$pageid]['parentid'];
		if($parentid != $oldparentid)
		{
			$this->move($pageid, $parentid, $oldparentid);
		}

		//$page['module'] = $this->module;
		$this->db->update($this->table, $page, "pageid=$pageid");
		if($setting) setting_set($this->table, "pageid=$pageid", $setting);
		/*if($this->module == 'page' && $page['type'] < 2)
		{*/
            $url = $this->page[$pageid]['child'] ? '' : '?module=page&action=manage&pageid='.$pageid;
			//$this->menu->update('pageid_'.$pageid, array('parentid'=>$this->menu->menuid('pageid_'.$parentid), 'name'=>$page['catname'], 'url'=>$url));
            //if($parentid) $this->menu->update('pageid_'.$parentid, array('url'=>''));
		/*}
		if($page['type'] == 2)
		{
			return true;
		}
		else
		{*/
			$this->url($pageid);
		//}
		return true;
	}

	function update_child($pageid)
	{
		global $page, $setting, $priv_groupid, $priv_roleid, $priv_role, $priv_group, $MODEL;
		if(!$pageid) return FALSE;
		
		$cat_childs = $page[$pageid]['arrchildid'];
		$cat_child = explode(',', $cat_childs);
		$t = $pageids = '';
		foreach($cat_child as $cid)
		{
			$C = cache_read('page_'.$cid.'.php');
			$setting['meta_title'] = $C['meta_title'];
			$setting['meta_keywords'] = $C['meta_keywords'];
			$setting['meta_description'] = $C['meta_description'];
			if($page[$pageid]['modelid']==$page[$cid]['modelid'] && $cid!=$pageid)
			{
				$pageids .= $t.$cid;
				setting_set($this->table, "pageid=$cid", $setting);
				$priv_group->update('pageid', $cid, $priv_groupid);
				$priv_role->update('pageid', $cid, $priv_roleid);
				$t = ',';
			}
		}
		if($pageids)
		{
			$this->db->query("UPDATE `".DB_PRE."page` c, `".DB_PRE.'c_'.$MODEL[$page[$pageid]['modelid']]['tablename']."` cc   SET `template`='$setting[template_show]' WHERE c.`pageid`=cc.`pageid` AND `pageid` IN ($pageids) ");
		}
		$this->repair();
		$this->cache();
		return true;
	}

	function link($pageid, $page)
	{
		$this->db->update($this->table, $page, "pageid=$pageid");
		$this->cache();
		return true;
	}

	function page($pageid, $page)
	{
		$this->db->update($this->table, $page, "pageid=$pageid");
		$this->cache();
		return true;
	}

	function delete($pageid)
	{
		global $MODEL,$MODULE, $c;
		if(!array_key_exists($pageid, $this->page)) return false;
		@set_time_limit(600);
		$arrparentid = $this->page[$pageid]['arrparentid'];
		$arrchildid = $this->page[$pageid]['arrchildid'];
		$pageids = explode(',', $arrchildid);
		if($this->page[$pageid]['type'] == 0)
		{
			$result = $this->db->query("SELECT pageid FROM ".DB_PRE."page WHERE pageid IN($arrchildid)");
			while($r = $this->db->fetch_array($result))
			{
				$c->delete($r['pageid']);
			}
			foreach($pageids as $id)
			{
				$modelid = $this->page[$id]['modelid'];
				if($this->page[$id]['type']) continue;
				$tablename = $MODEL[$modelid]['tablename'];
				if($tablename && $this->db->table_exists(DB_PRE.'c_'.$tablename))
				{
					$this->db->query("DELETE `".DB_PRE."page`,`".DB_PRE."c_$tablename` FROM `".DB_PRE."page`,`".DB_PRE."c_$tablename` WHERE `".DB_PRE."page`.pageid=`".DB_PRE."c_$tablename`.pageid AND `".DB_PRE."page`.pageid='$id'");
				}
				//if($this->module == 'page' && $this->page[$id]['type'] < 2) $this->menu->update('pageid_'.$id);
				//unset($this->page[$id]);
			}
		}
		else
		{
			//$this->menu->update('pageid_'.$pageid);
		}
		$this->db->query("DELETE FROM `$this->table` WHERE `pageid` IN($arrchildid)");
		if($arrparentid)
		{
			$arrparentids = explode(',', $arrparentid);
			foreach($arrparentids as $id)
			{
				if($id == 0) continue;
				$arrchildid = $this->get_arrchildid($id);
				$child = is_numeric($arrchildid) ? 0 : 1;
				$this->db->query("UPDATE `$this->table` SET `arrchildid`='$arrchildid', `child`='$child' WHERE `pageid`='$id'");
				//if($this->module == 'page' && $this->page[$id]['type'] < 2) $this->menu->update('pageid_'.$id, array('isfolder'=>$child));
			}
		}
		$this->cache();
		return true;
	}

	function listorder($listorder)
	{
		if(!is_array($listorder)) return FALSE;
		foreach($listorder as $pageid=>$value)
		{
			$value = intval($value);
			$this->db->query("UPDATE `$this->table` SET listorder=$value WHERE pageid=$pageid");
		}
		$this->cache();
		return TRUE;
	}

	function recycle($pageid)
	{
		$modelid = $this->page[$pageid]['modelid'];
		$m = cache_read('model_'.$modelid.'.php');
		$this->db->query("DELETE FROM `".DB_PRE."page` ,`".DB_PRE."c_".$m['tablename']."` USING `".DB_PRE."page`,`".DB_PRE."c_".$m['tablename']."` WHERE `".DB_PRE."page`.pageid='$pageid' AND `".DB_PRE."page`.pageid=`".DB_PRE."c_".$m['tablename']."`.pageid");
		return TRUE;
	}

	function listinfo($parentid = -1)
	{
		$pages = array();
		$where = $parentid > -1 ? " AND parentid='$parentid'" : ' 1 = 1';
		$result = $this->db->query("SELECT `pageid` FROM `$this->table` WHERE $where ORDER BY `listorder`,`pageid`");
		while($r = $this->db->fetch_array($result))
		{
			$pages[$r['pageid']] = $this->get($r['pageid']);
		}
		$this->db->free_result($result);
		return $pages;
	}

	function repair()
	{
		@set_time_limit(600);

		if(is_array($this->page))
		{
			foreach($this->page as $pageid => $cat)
			{
				if($pageid == 0) continue;
				$arrparentid = $this->get_arrparentid($pageid);
				$parentdir = $this->get_parentdir($pageid);
				$arrchildid = $this->get_arrchildid($pageid);
				$child = is_numeric($arrchildid) ? 0 : 1;
				$this->db->query("UPDATE `$this->table` SET arrparentid='$arrparentid',parentdir='$parentdir',arrchildid='$arrchildid',child='$child' WHERE pageid=$pageid");
				
				// 当栏目模型除了外部链接的时候都需要更新一下栏目链接
				if($cat['type']!=2) $this->url($pageid);
			}
		}
		//$this->cache();
		return TRUE;
	}

	function join($sourcepageid, $targetpageid)
	{
		$arrchildid = $this->page[$sourcepageid]['arrchildid'];
		$arrparentid = $this->page[$sourcepageid]['arrparentid'];

		$this->db->query("DELETE FROM `$this->table` WHERE `pageid` IN ($arrchildid)");

		$this->db->query("UPDATE ".DB_PRE."page set pageid='$targetpageid' WHERE pageid IN ($arrchildid)");

		$pageids = explode(',', $arrchildid);
		//foreach($pageids as $id)
//		{
//			$this->db->query("DELETE FROM ".DB_PRE."menu WHERE keyid='pageid_$id' LIMIT 1");
//			unset($this->page[$id]);
//		}

		if($arrparentid)
		{
			$arrparentids = explode(',', $arrparentid);
			foreach($arrparentids as $id)
			{
				if($id == 0) continue;
				$arrchildid = $this->get_arrchildid($id);
				$child = is_numeric($arrchildid) ? 0 : 1;
				$this->db->query("UPDATE `$this->table` SET arrchildid='$arrchildid',child=$child WHERE pageid='$id'");
			}
		}

		$this->cache();
		return true;
	}

	function get_arrparentid($pageid, $arrparentid = '', $n = 1)
	{
		if($n > 5 || !is_array($this->page) || !isset($this->page[$pageid])) return false;
		$parentid = $this->page[$pageid]['parentid'];
		$arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
		if($parentid)
		{
			$arrparentid = $this->get_arrparentid($parentid, $arrparentid, ++$n);
		}
		else
		{
			$this->page[$pageid]['arrparentid'] = $arrparentid;
		}
		return $arrparentid;
	}

	function get_arrchildid($pageid)
	{
		$arrchildid = $pageid;
		if(is_array($this->page))
		{
			foreach($this->page as $id => $cat)
			{
				if($cat['parentid'] && $id != $pageid)
				{
					$arrparentids = explode(',', $cat['arrparentid']);
					if(in_array($pageid, $arrparentids)) $arrchildid .= ','.$id;
				}
			}
		}
		return $arrchildid;
	}

	function get_parentdir($pageid)
	{
		if($this->page[$pageid]['parentid']==0) return '';
		$arrparentid = $this->page[$pageid]['arrparentid'];
		$arrparentid = explode(',', $arrparentid);
		$arrcatdir = array();
		foreach($arrparentid as $id)
		{
			if($id==0) continue;
			$arrcatdir[] = $this->page[$id]['catdir'];
		}
		return implode('/', $arrcatdir);
	}

	function move($pageid, $parentid, $oldparentid)
	{
		$arrchildid = $this->page[$pageid]['arrchildid'];
		$oldarrparentid = $this->page[$pageid]['arrparentid'];
		$oldparentdir = $this->page[$pageid]['parentdir'];
		$child = $this->page[$pageid]['child'];
		$oldarrparentids = explode(',', $this->page[$pageid]['arrparentid']);
		$arrchildids = explode(',', $this->page[$pageid]['arrchildid']);
		if(in_array($parentid, $arrchildids)) return FALSE;
		$this->page[$pageid]['parentid'] = $parentid;
		if($child)
		{
			foreach($arrchildids as $cid)
			{
				if($cid==$pageid) continue;
				$newarrparentid = $this->get_arrparentid($cid);
				$this->page[$cid]['arrparentid'] = $newarrparentid;
				$newparentdir = $this->get_parentdir($cid);
				$this->db->query("UPDATE `$this->table` SET arrparentid='$newarrparentid',parentdir='$newparentdir' WHERE pageid='$cid'");
			}
		}
		if($parentid)
		{
			$arrparentid = $this->page[$parentid]['arrparentid'].",".$parentid;
			$this->page[$pageid]['arrparentid'] = $arrparentid;
			$parentdir = $this->page[$parentid]['parentdir'].$r['catdir']."/";
			$arrparentids = explode(",", $arrparentid);
			foreach($arrparentids as $pid)
			{
				if($pid==0) continue;
				$newarrchildid = $this->get_arrchildid($pid);
				$this->db->query("UPDATE `$this->table` SET arrchildid='$newarrchildid',child=1 WHERE pageid=$pid");
			}
		}
		else
		{
			$arrparentid = 0;
			$parentdir = '/';
			$this->page[$pageid]['arrparentid'] = $arrparentid;
		}
		$this->db->query("UPDATE `$this->table` SET arrparentid='$arrparentid',parentdir='$parentdir' WHERE pageid=$pageid");
		if($oldparentid)
		{
			foreach($oldarrparentids as $pid)
			{
				if($pid==0) continue;
				$oldarrchildid = $this->get_arrchildid($pid);
				$child = is_numeric($oldarrchildid) ? 0 : 1;
				$this->db->query("UPDATE `$this->table` SET arrchildid='$oldarrchildid' ,child=$child WHERE pageid=$pid");
			}
		}
		return TRUE;
	}

	function depth($pageid)
	{
		return (substr_count($this->page[$pageid]['arrparentid'], ',') + 1);
	}

	function url($pageid, $is_update = 1)
	{
		global $MODEL, $page,$SETTING,$module;
		$data = $this->get($pageid);
		if(!$data) return false;
		$this->u->page[$pageid] = $data;
		
		// 栏目是外部链接不用url这一步
		if($this->page[$pageid]['type'] == 2) return false;
		
		// 更新该栏目信息缓存
		cache_write('page_'.$pageid.'.php', $data);
		
		
		if($data['catdir']){
			$url = $data['parentdir'] ? "?module=$module&catdir=$data[catdir]&parentdir=$data[parentdir]" : "?module=$module&catdir=$data[catdir]";
		}
		else{
			$url = "?module=$module&pageid=$pageid";
		}
		
		// 得到URL链接
		//$u = load('url.class.php','url');
		//$url = $u->page($pageid);
		
		
		
		$this->db->query("UPDATE `$this->table` SET url='$url' WHERE pageid=$pageid");
		
		//echo "UPDATE `$this->table` SET url='$url' WHERE pageid=$pageid";
		
		
		
		/*cache_write('page_'.$pageid.'.php', $data);
		if($MODEL[$this->page[$pageid]['modelid']]['ishtml'])
		{
			if(!preg_match('/:\/\//',$data['url']))
			{
				$url_a = $this->u->page($pageid);
				$url = $url_a[1];
			}
			else
			{
				$url = $data['url'];
			}
		}
		else
		{
			$url_a = $this->u->page($pageid);
			$url = $url_a[1];
		}
		$url = preg_replace('/index\.[a-z]{3,5}$/', '', $url);
		if($is_update)
		{
			$pages_c = array();
			$result = $this->db->query("SELECT * FROM `$this->table` WHERE `module`='$this->module'");
			while($r = $this->db->fetch_array($result))
			{
				$pages_c[$r['pageid']] = $r;
			}
			if($data['parentid']==0 || (preg_match('/:\/\//', $url) && (substr_count($url, '/')<4)))
			{
				$this->db->query("UPDATE `$this->table` SET url='$url' WHERE pageid=$pageid");
			}
			$arrchildid = $data['arrchildid'];
			$arrchild = explode(',',$arrchildid);
			foreach($arrchild AS $k)
			{
				if($pages_c[$k]['type'] == 2) continue;
				$parentdir = $second_domain = $parentid_arr = '';
				if($pages_c[$k]['modelid'])
				{
					if(!$MODEL[$pages_c[$k]['modelid']]['ishtml'])
					{
						$url_a = $this->u->page($k);
						$caturl = $url_a[1];
						$this->db->query("UPDATE `$this->table` SET url='$caturl' WHERE pageid=$k");
						continue;
					}
				}
				else
				{
					$child_array_data = $this->get($k);
					if(!$child_array_data['ishtml'])
					{
						$url_a = $this->u->page($k);
						$caturl = $url_a[1];
						$this->db->query("UPDATE `$this->table` SET url='$caturl' WHERE pageid=$k");
						continue;
					}
				}
				$url_a = $this->u->page($k);
				$caturl = $url_a[1];
				
				// 原始版本
				// 路径为 about/joinus/
				//$caturl = preg_replace('/index\.[a-z]{3,5}$/', '', $caturl);
				
				// 现在版本
				// 路径为 about/joinus
				$caturl = preg_replace('/\/index\.[a-z]{3,5}$/', '', $caturl);
				
				$this->db->query("UPDATE `$this->table` SET url='$caturl' WHERE pageid=$k");
			}
			unset($url);
		}*/
		return $url;
	}
	

	function count($pageid, $status = null)
	{
        if(!isset($this->page[$pageid])) return false;
		$where = '';
		$where .= $this->page[$pageid]['child'] ? "AND `pageid` IN(".$this->page[$pageid]['arrchildid'].") " : "AND `pageid`=$pageid ";
        $where .= $status == null ? '' : "AND status='$status' ";
		if($where) $where = ' WHERE '.substr($where, 3);
		return cache_count("SELECT COUNT(*) AS `count` FROM `".DB_PRE."page` $where");
	}

	function cache()
	{
		@set_time_limit(600);
		cache_page();
		cache_common();
	}
}
?>